﻿using System;
using System.Data.SqlClient;
using Company.BLL.WS.KDT;
using System.Data;
using Microsoft.Practices.EnterpriseLibrary.Data.Sql;
using Microsoft.Practices.EnterpriseLibrary.Data;
using System.Xml;

namespace Company.BLL.KDT
{
    public partial class ToKhaiMauDich
    {
        private HangMauDichCollection _HMDCollection = new HangMauDichCollection();
        private ChungTuCollection _chungtuCollection = new ChungTuCollection();
        public HangMauDichCollection HMDCollection
        {
            set { this._HMDCollection = value; }
            get { return this._HMDCollection; }
        }
        public ChungTuCollection ChungTuTKCollection
        {
            set { this._chungtuCollection = value; }
            get { return this._chungtuCollection; }
        }

        //-----------------------------------------------------------------------------------------

        public ToKhaiMauDichInfo ExportToInfo()
        {
            ToKhaiMauDichInfo entityInfo = new ToKhaiMauDichInfo();            
            entityInfo.SoTiepNhan = this._SoTiepNhan;
            entityInfo.NgayTiepNhan = this._NgayTiepNhan;
            entityInfo.MaHaiQuan = this._MaHaiQuan;
            entityInfo.SoToKhai = this._SoToKhai;
            entityInfo.MaLoaiHinh = this._MaLoaiHinh;
            entityInfo.NgayDangKy = this._NgayDangKy;
            entityInfo.MaDoanhNghiep = this._MaDoanhNghiep;
            entityInfo.TenDoanhNghiep = this._TenDoanhNghiep;
            entityInfo.MaDaiLyTTHQ = this._MaDaiLyTTHQ;
            entityInfo.TenDaiLyTTHQ = this._TenDaiLyTTHQ;
            entityInfo.TenDonViDoiTac = this._TenDonViDoiTac;
            entityInfo.ChiTietDonViDoiTac = this._ChiTietDonViDoiTac;
            entityInfo.SoGiayPhep = this._SoGiayPhep;
            entityInfo.NgayGiayPhep = this._NgayGiayPhep;
            entityInfo.NgayHetHanGiayPhep = this._NgayHetHanGiayPhep;
            entityInfo.SoHopDong = this._SoHopDong;
            entityInfo.NgayHopDong = this._NgayHopDong;
            entityInfo.NgayHetHanHopDong = this._NgayHetHanHopDong;
            entityInfo.SoHoaDonThuongMai = this._SoHoaDonThuongMai;
            entityInfo.NgayHoaDonThuongMai = this._NgayHoaDonThuongMai;
            entityInfo.PTVT_ID = this._PTVT_ID;
            entityInfo.SoHieuPTVT = this._SoHieuPTVT;
            entityInfo.NgayDenPTVT = this._NgayDenPTVT;
            entityInfo.QuocTichPTVT_ID = this._QuocTichPTVT_ID;
            entityInfo.LoaiVanDon = this._LoaiVanDon;
            entityInfo.SoVanDon = this._SoVanDon;
            entityInfo.NgayVanDon = this._NgayVanDon;
            entityInfo.NuocXK_ID = this._NuocXK_ID;
            entityInfo.NuocNK_ID = this._NuocNK_ID;
            entityInfo.DiaDiemXepHang = this._DiaDiemXepHang;
            entityInfo.CuaKhau_ID = this._CuaKhau_ID;
            entityInfo.DKGH_ID = this._DKGH_ID;
            entityInfo.NguyenTe_ID = this._NguyenTe_ID;
            entityInfo.TyGiaTinhThue = this._TyGiaTinhThue;
            entityInfo.TyGiaUSD = this._TyGiaUSD;
            entityInfo.PTTT_ID = this._PTTT_ID;
            entityInfo.SoHang = this._SoHang;
            entityInfo.SoLuongPLTK = this._SoLuongPLTK;
            entityInfo.TenChuHang = this._TenChuHang;
            entityInfo.SoContainer20 = this._SoContainer20;
            entityInfo.SoContainer40 = this._SoContainer40;
            entityInfo.SoKien = this._SoKien;
            entityInfo.TrongLuong = this._TrongLuong;
            entityInfo.TongTriGiaKhaiBao = this._TongTriGiaKhaiBao;
            entityInfo.TongTriGiaTinhThue = this._TongTriGiaTinhThue;
            entityInfo.TrangThaiXuLy = this._TrangThaiXuLy;
            entityInfo.LoaiToKhaiGiaCong = this._LoaiToKhaiGiaCong;
            entityInfo.LePhiHaiQuan = this._LePhiHaiQuan;
            entityInfo.PhiBaoHiem = this._PhiBaoHiem;
            entityInfo.PhiVanChuyen = this._PhiVanChuyen;
            entityInfo.PhiXepDoHang = this._PhiXepDoHang;
            entityInfo.PhiKhac = this._PhiKhac;
            entityInfo.CanBoDangKy = this._CanBoDangKy;
            entityInfo.QuanLyMay = this._QuanLyMay;
            entityInfo.LoaiHangHoa = this.LoaiHangHoa;
            entityInfo.GiayTo = this.GiayTo;
            return entityInfo;
        }

        //-----------------------------------------------------------------------------------------

        public void Load(ToKhaiMauDichInfo entityInfo)
        {            
            this._SoTiepNhan = entityInfo.SoTiepNhan;
            this._NgayTiepNhan = entityInfo.NgayTiepNhan;
            this._MaHaiQuan = entityInfo.MaHaiQuan;
            this._SoToKhai = entityInfo.SoToKhai;
            this._MaLoaiHinh = entityInfo.MaLoaiHinh;
            this._NgayDangKy = entityInfo.NgayDangKy;
            this._MaDoanhNghiep = entityInfo.MaDoanhNghiep;
            this._TenDoanhNghiep = entityInfo.TenDoanhNghiep;
            this._MaDaiLyTTHQ = entityInfo.MaDaiLyTTHQ;
            this._TenDaiLyTTHQ = entityInfo.TenDaiLyTTHQ;
            this._TenDonViDoiTac = entityInfo.TenDonViDoiTac;
            this._ChiTietDonViDoiTac = entityInfo.ChiTietDonViDoiTac;
            this._SoGiayPhep = entityInfo.SoGiayPhep;
            this._NgayGiayPhep = entityInfo.NgayGiayPhep;
            this._NgayHetHanGiayPhep = entityInfo.NgayHetHanGiayPhep;
            this._SoHopDong = entityInfo.SoHopDong;
            this._NgayHopDong = entityInfo.NgayHopDong;
            this._NgayHetHanHopDong = entityInfo.NgayHetHanHopDong;
            this._SoHoaDonThuongMai = entityInfo.SoHoaDonThuongMai;
            this._NgayHoaDonThuongMai = entityInfo.NgayHoaDonThuongMai;
            this._PTVT_ID = entityInfo.PTVT_ID;
            this._SoHieuPTVT = entityInfo.SoHieuPTVT;
            this._NgayDenPTVT = entityInfo.NgayDenPTVT;
            this._QuocTichPTVT_ID = entityInfo.QuocTichPTVT_ID;
            this._LoaiVanDon = entityInfo.LoaiVanDon;
            this._SoVanDon = entityInfo.SoVanDon;
            this._NgayVanDon = entityInfo.NgayVanDon;
            this._NuocXK_ID = entityInfo.NuocXK_ID;
            this._NuocNK_ID = entityInfo.NuocNK_ID;
            this._DiaDiemXepHang = entityInfo.DiaDiemXepHang;
            this._CuaKhau_ID = entityInfo.CuaKhau_ID;
            this._DKGH_ID = entityInfo.DKGH_ID;
            this._NguyenTe_ID = entityInfo.NguyenTe_ID;
            this._TyGiaTinhThue = entityInfo.TyGiaTinhThue;
            this._TyGiaUSD = entityInfo.TyGiaUSD;
            this._PTTT_ID = entityInfo.PTTT_ID;
            this._SoHang = entityInfo.SoHang;
            this._SoLuongPLTK = entityInfo.SoLuongPLTK;
            this._TenChuHang = entityInfo.TenChuHang;
            this._SoContainer20 = entityInfo.SoContainer20;
            this._SoContainer40 = entityInfo.SoContainer40;
            this._SoKien = entityInfo.SoKien;
            this._TrongLuong = entityInfo.TrongLuong;
            this._TongTriGiaKhaiBao = entityInfo.TongTriGiaKhaiBao;
            this._TongTriGiaTinhThue = entityInfo.TongTriGiaTinhThue;
            this._TrangThaiXuLy = entityInfo.TrangThaiXuLy;
            this._LoaiToKhaiGiaCong = entityInfo.LoaiToKhaiGiaCong;
            this._LePhiHaiQuan = entityInfo.LePhiHaiQuan;
            this._PhiBaoHiem = entityInfo.PhiBaoHiem;
            this._PhiVanChuyen = entityInfo.PhiVanChuyen;
            this._PhiXepDoHang = entityInfo.PhiXepDoHang;
            this._PhiKhac = entityInfo.PhiKhac;
            this._CanBoDangKy = entityInfo.CanBoDangKy;
            this._QuanLyMay = entityInfo.QuanLyMay;
        }
        private void InsertUpdateFull(SqlTransaction transaction)
        {
            if (this.ID == 0)
                this.ID = this.InsertTransaction(transaction);
            else
                this.UpdateTransaction(transaction);

            foreach (HangMauDich hmdDetail in this.HMDCollection)
            {
                if (hmdDetail.ID == 0)
                {
                    hmdDetail.TKMD_ID = this.ID;
                    hmdDetail.ID = hmdDetail.InsertTransaction(transaction);
                }
                else
                {
                    hmdDetail.UpdateTransaction(transaction);
                }
            }
            foreach (ChungTu ct in this.ChungTuTKCollection)
            {
                ct.InsertUpdateTransaction(transaction);
            }
        }
        public bool InsertUpdateFull()
        {
            bool ret;
            SqlDatabase db = (SqlDatabase)DatabaseFactory.CreateDatabase();
            using (SqlConnection connection = (SqlConnection)db.CreateConnection())
            {
                connection.Open();
                SqlTransaction transaction = connection.BeginTransaction();
                try
                {
                    if (this.ID == 0)
                        this.ID = this.InsertTransaction(transaction);
                    else
                        this.UpdateTransaction(transaction);

                    foreach (HangMauDich hmd in this.HMDCollection)
                    {
                        if (hmd.ID == 0)
                        {
                            hmd.TKMD_ID = this.ID;
                            hmd.ID = hmd.InsertTransaction(transaction);
                        }
                        else
                        {
                            hmd.UpdateTransaction(transaction);
                        }
                    }
                    int i = 1;
                    foreach (ChungTu ct in this.ChungTuTKCollection)
                    {
                        ct.STTHang = i++;
                        if (ct.ID == 0)
                        {
                            ct.Master_ID = this.ID;
                            ct.ID=ct.InsertTransaction(transaction);
                        }
                        else
                        {
                            ct.UpdateTransaction(transaction);
                        }
                    }                   
                    transaction.Commit();
                    ret = true;
                }
                catch (Exception ex)
                {
                    transaction.Rollback();
                    throw new Exception(ex.Message);
                }
                finally
                {
                    connection.Close();
                }
            }
            return ret;
        }
        //-----------------------------------------------------------------------------------------

        public void LoadHMDCollection()
        {
            HangMauDich hmd = new HangMauDich();
            hmd.TKMD_ID = this.ID;
            this.HMDCollection = hmd.SelectCollectionBy_TKMD_ID();
        }
        public void LoadChungTuTKCollection()
        {
            ChungTu ct = new ChungTu();
            ct.Master_ID = this.ID;
            this.ChungTuTKCollection = ct.SelectCollectionBy_Master_ID();
        }
        //-----------------------------------------------------------------------------------------

        public long InsertFull()
        {
            long ret;
            using (SqlConnection connection = (SqlConnection) db.CreateConnection())
            {
                connection.Open();
                SqlTransaction transaction = connection.BeginTransaction();
                try
                {
                    long ret01 = this.InsertTransaction(transaction);
                    foreach (HangMauDich hmd in this.HMDCollection)
                    {
                        hmd.TKMD_ID = ret01;
                        hmd.InsertTransaction(transaction);
                    }
                    foreach (ChungTu ct in this.ChungTuTKCollection)
                    {
                        ct.Master_ID = ret01;
                        ct.InsertTransaction(transaction);
                    }
                    if (ret01 > 0)
                    {
                        transaction.Commit();
                        ret = ret01;
                    }
                    else
                    {
                        transaction.Rollback();
                        ret = 0;
                    }
                }
                catch (Exception ex)
                {
                    transaction.Rollback();
                    throw new Exception(ex.Message);
                }
                finally
                {
                    connection.Close();
                }
            }
            return ret;
        }
        public long UpdateFull()
        {
            long ret;
            using (SqlConnection connection = (SqlConnection)db.CreateConnection())
            {
                connection.Open();
                SqlTransaction transaction = connection.BeginTransaction();
                try
                {
                    long ret01 = this.UpdateTransaction(transaction);
                    //HangMauDich hmd1 = new HangMauDich();
                    //ToKhaiMauDich tk = new ToKhaiMauDich();
                    //tk.ID = this.ID;
                    //tk.LoadHMDCollection();
                    //hmd1.DeleteCollection(tk.HMDCollection, transaction);
                    foreach (HangMauDich hmd in this.HMDCollection)
                    {
                        if (hmd.Load()) hmd.UpdateTransaction(transaction);
                        else
                        {
                            hmd.TKMD_ID = this.ID;
                            hmd.InsertTransaction(transaction);
                        }
                    }
                    foreach (ChungTu ct in this.ChungTuTKCollection)
                    {
                        ct.Master_ID = ret01;
                        ct.InsertTransaction(transaction);
                    }
                    if (ret01 > 0)
                    {
                        transaction.Commit();
                        ret = ret01;
                    }
                    else
                    {
                        transaction.Rollback();
                        ret = 0;
                    }
                }
                catch (Exception ex)
                {
                    transaction.Rollback();
                    throw new Exception(ex.Message);
                }
                finally
                {
                    connection.Close();
                }
            }
            return ret;
        }

        #region Web Services

        /// <summary>
        /// Khai báo tờ khai đến Hải quan.
        /// </summary>
        /// <returns>Số tiếp nhận</returns>
        public long WSSend()
        {            
            // this.LoadHMDCollection();
            KDTService service = new KDTService();
            // Lấy thông tin tờ khai.
            ToKhaiMauDichInfo tkmdInfo = this.ExportToInfo();

            // Lấy thông tin hàng hóa.
            int count = this.HMDCollection.Count;
            tkmdInfo.HMDInfoCollection = new HangMauDichInfo[count];
            tkmdInfo.ChungTuCollection=new ChungTuInfo[this.ChungTuTKCollection.Count];
            
            int i = 0;
            foreach (HangMauDich hmd in this.HMDCollection)
            {
                tkmdInfo.HMDInfoCollection[i] = hmd.ExportToInfo();                
                i++;
            }
            i = 0;
            foreach (ChungTu ct in this.ChungTuTKCollection)
            {
                tkmdInfo.ChungTuCollection[i] = ct.ExportToInfo();
                i++;
            }
            
            long soTiepNhan = service.ToKhaiMauDich_Send(tkmdInfo);

            // Cập nhật thông tin vào CSDL.
            if (soTiepNhan > 0)
            {
                this._SoTiepNhan = soTiepNhan;
                this._TrangThaiXuLy = BLL.TrangThaiXuLy.CHO_DUYET;
                this._NgayTiepNhan = DateTime.Now;
                
            }
            this.InsertUpdateFull();
            return soTiepNhan;
        }

        public bool WSCancel()
        {
            KDTService service = new KDTService();
            if (service.ToKhaiMauDich_Cancel(this._SoTiepNhan, this._MaHaiQuan, this._MaDoanhNghiep, this._NgayTiepNhan.Year))
            {
                this._SoTiepNhan = 0;
                this.TrangThaiXuLy = -1;
                this.Update();
                return true;
            }
            return false;
        }
        public void WSRequestStatus()
        {
            // Các bước thực hiện:
            // 1. Lấy thông tin từ Web Service.
            // 2. Cập nhật thông tin đăng ký.
            // 3. Xóa danh sách hàng chuyển tiếp cũ.
            // 4. Cập nhật lại danh sách hàng chuyển tiếp mới mới.
            // 5. Nếu trạng thái: Đã duyệt chính thức thì chuyển danh sách sản phẩm đã duyệt sang phân hệ SXXK.

            // 1. Lấy danh sách từ Web Service.
            KDTService kdtService = new KDTService();
            string[] stXML = kdtService.ToKhaiMauDich_RequestStatus(this.SoTiepNhan, this.MaHaiQuan, this.MaDoanhNghiep, Convert.ToInt16(this.NgayTiepNhan.Year));
            DataSet ds = new DataSet();
            System.IO.StringReader xmlSR = new System.IO.StringReader(stXML[3]);
            ds.ReadXmlSchema(xmlSR);
            xmlSR = new System.IO.StringReader(stXML[0]);
            ds.ReadXml(xmlSR);     
            DataTable dt = ds.Tables[0];
            DataTable dtChungTu = null;
            if (stXML[1] != null)
            {
                DataSet dsChungTu = new DataSet();
                xmlSR = new System.IO.StringReader(stXML[4]);
                dsChungTu.ReadXmlSchema(xmlSR);
                xmlSR = new System.IO.StringReader(stXML[1]);
                dsChungTu.ReadXml(xmlSR);
                dtChungTu = dsChungTu.Tables[0];
            }
            else
            {
                dtChungTu = new DataTable();
            }
            if (dt.Rows.Count > 0)
            {
                SqlDatabase db = (SqlDatabase)DatabaseFactory.CreateDatabase();
                using (SqlConnection connection = (SqlConnection)db.CreateConnection())
                {
                    connection.Open();
                    SqlTransaction transaction = connection.BeginTransaction();
                    try
                    {
                        // 2. Cập nhật thông tin đăng ký.
                        this.TrangThaiXuLy = Convert.ToInt32(dt.Rows[0]["TrangThaiXL"]);
                        this.MaLoaiHinh = Convert.ToString(dt.Rows[0]["Ma_LH"]);
                        if (dt.Rows[0]["DaiLy_TTHQ"].ToString() != "")
                            this.MaDaiLyTTHQ = Convert.ToString(dt.Rows[0]["DaiLy_TTHQ"]);
                        if (dt.Rows[0]["TenDLTTHQ"].ToString() != "")
                            this.TenDaiLyTTHQ = Convert.ToString(dt.Rows[0]["TenDLTTHQ"]);
                        if (dt.Rows[0]["DV_DT"].ToString() != "")
                            this.TenDonViDoiTac = Convert.ToString(dt.Rows[0]["DV_DT"]);
                        if (dt.Rows[0]["So_GP"].ToString() != "")
                            this.SoGiayPhep = Convert.ToString(dt.Rows[0]["So_GP"]);
                        if (dt.Rows[0]["So_TK"].ToString() != "")
                            this.SoToKhai = Convert.ToInt32(dt.Rows[0]["So_TK"]);
                        if (dt.Rows[0]["Ngay_DK"].ToString() != "")
                            this.NgayDangKy = Convert.ToDateTime(dt.Rows[0]["Ngay_DK"]);
                        if(dt.Rows[0]["Ngay_GP"].ToString()!="")
                            this.NgayGiayPhep = Convert.ToDateTime(dt.Rows[0]["Ngay_GP"].ToString());
                        if (dt.Rows[0]["Ngay_HHGP"].ToString() != "")
                            this.NgayHetHanGiayPhep = Convert.ToDateTime(dt.Rows[0]["Ngay_GP"].ToString());
                        this.SoHopDong = Convert.ToString(dt.Rows[0]["So_HD"].ToString());
                        if (dt.Rows[0]["Ngay_HD"].ToString() != "")
                            this.NgayHopDong = Convert.ToDateTime(dt.Rows[0]["Ngay_HD"].ToString());
                        if (dt.Rows[0]["Ngay_HHHD"].ToString() != "")
                            this.NgayHetHanHopDong = Convert.ToDateTime(dt.Rows[0]["Ngay_HHHD"].ToString());
                        this.SoHoaDonThuongMai = Convert.ToString(dt.Rows[0]["So_HDTM"]);
                        if (dt.Rows[0]["Ngay_HDTM"].ToString() != "")
                            this.NgayHoaDonThuongMai = Convert.ToDateTime(dt.Rows[0]["Ngay_HDTM".ToString()]);
                        this.PTVT_ID = Convert.ToString(dt.Rows[0]["Ma_PTVT"]);
                        this.SoHieuPTVT = Convert.ToString(dt.Rows[0]["Ten_PTVT"]);
                        if (dt.Rows[0]["NgayDen"].ToString() != "")
                            this.NgayDenPTVT = Convert.ToDateTime(dt.Rows[0]["NgayDen"].ToString());
                        this.LoaiVanDon = Convert.ToString(dt.Rows[0]["Van_Don"]);
                        if (dt.Rows[0]["Ngay_VanDon"].ToString() != "")
                            this.NgayVanDon = Convert.ToDateTime(dt.Rows[0]["Ngay_VanDon"].ToString());
                        this.NuocXK_ID = Convert.ToString(dt.Rows[0]["Nuoc_XK"]);
                        this.NuocNK_ID = Convert.ToString(dt.Rows[0]["Nuoc_NK"]);
                        this.DiaDiemXepHang = Convert.ToString(dt.Rows[0]["CangNN"]);
                        this.DKGH_ID = Convert.ToString(dt.Rows[0]["Ma_GH"]);
                        this.CuaKhau_ID = Convert.ToString(dt.Rows[0]["Ma_CK"]);
                        this.NguyenTe_ID = Convert.ToString(dt.Rows[0]["Ma_NT"]);
                        this.TyGiaTinhThue = Convert.ToDecimal(dt.Rows[0]["TyGia_VND"]);
                        this.TyGiaUSD = Convert.ToDecimal(dt.Rows[0]["TyGia_USD"]);
                        this.PTTT_ID = Convert.ToString(dt.Rows[0]["Ma_PTTT"]);
                        if (dt.Rows[0]["SoHang"].ToString()!="")
                            this.SoHang = Convert.ToInt16(dt.Rows[0]["SoHang"]);
                        if (dt.Rows[0]["So_PLTK"].ToString() != "")
                            this.SoLuongPLTK = Convert.ToInt16(dt.Rows[0]["So_PLTK"]);
                        this.TenChuHang = Convert.ToString(dt.Rows[0]["TenCH"]);
                        if (dt.Rows[0]["So_Container"].ToString() != "")
                            this.SoContainer20 = Convert.ToDecimal(dt.Rows[0]["So_Container"]);
                        if (dt.Rows[0]["So_Container40"].ToString() != "")
                            this.SoContainer40 = Convert.ToDecimal(dt.Rows[0]["So_Container40"]);
                        if (dt.Rows[0]["So_Kien"].ToString() != "")
                            this.SoKien = Convert.ToDecimal(dt.Rows[0]["So_Kien"]);
                        if (dt.Rows[0]["Tr_Luong"].ToString() != "") 
                            this.TrongLuong = Convert.ToDecimal(dt.Rows[0]["Tr_Luong"]);
                        this.CanBoDangKy = Convert.ToString(dt.Rows[0]["User_DK"]);
                        if (dt.Rows[0]["TongTGKB"].ToString() != "")
                            this.TongTriGiaKhaiBao = Convert.ToDecimal(dt.Rows[0]["TongTGKB"]);
                        if (dt.Rows[0]["TongTGTT"].ToString() != "")
                            this.TongTriGiaTinhThue = Convert.ToDecimal(dt.Rows[0]["TongTGTT"]);
                        if (dt.Rows[0]["QLMay"].ToString() != "")
                            this.QuanLyMay = Convert.ToBoolean(dt.Rows[0]["QLMay"].ToString());
                        this.LoaiToKhaiGiaCong = Convert.ToString(dt.Rows[0]["LoaiTKGC"]);
                        this.MaDoanhNghiep = Convert.ToString(dt.Rows[0]["Ma_DV"]);
                        if (dt.Rows[0]["LePhi_HQ"].ToString() != "")
                            this.LePhiHaiQuan = Convert.ToDecimal(dt.Rows[0]["LePhi_HQ"]);
                        if (dt.Rows[0]["Phi_BH"].ToString() != "")
                            this.PhiBaoHiem = Convert.ToDecimal(dt.Rows[0]["Phi_BH"]);
                        if (dt.Rows[0]["Phi_VC"].ToString() != "")
                        this.PhiVanChuyen = Convert.ToDecimal(dt.Rows[0]["Phi_VC"]);
                        this.GiayTo = dt.Rows[0]["GiayTo"].ToString();
                        //neu da duyet thi dua vao phan jhe da duyet
                    BLL.SXXK.ToKhai.ToKhaiMauDich tkDuyet = new Company.BLL.SXXK.ToKhai.ToKhaiMauDich();                     
                    if (this.TrangThaiXuLy == BLL.TrangThaiXuLy.DA_DUYET)
                    {
                        //cap nhat thong tin phan luong neu co
                        if (stXML[2] != null)
                            this.PhanLuong = stXML[2];
                        tkDuyet.MaDoanhNghiep = this.MaDoanhNghiep;
                        tkDuyet.PhanLuong = stXML[2];
                        tkDuyet.MaHaiQuan = this.MaHaiQuan;
                        tkDuyet.MaLoaiHinh = this.MaLoaiHinh;
                        tkDuyet.NamDangKy = (short)this.NgayDangKy.Year;
                        tkDuyet.NgayDangKy = this.NgayDangKy;
                        tkDuyet.SoToKhai = this.SoToKhai;
                        tkDuyet.TenDonViDoiTac = this.TenDonViDoiTac;
                        if (dt.Rows[0]["Ngay_GP"].ToString() != "")
                            tkDuyet.NgayGiayPhep = Convert.ToDateTime(dt.Rows[0]["Ngay_GP"].ToString());
                        if (dt.Rows[0]["Ngay_HHGP"].ToString() != "")
                            tkDuyet.NgayHetHanGiayPhep = Convert.ToDateTime(dt.Rows[0]["Ngay_GP"].ToString());
                        tkDuyet.SoHopDong = Convert.ToString(dt.Rows[0]["So_HD"].ToString());
                        if (dt.Rows[0]["Ngay_HD"].ToString() != "")
                            tkDuyet.NgayHopDong = Convert.ToDateTime(dt.Rows[0]["Ngay_HD"].ToString());
                        if (dt.Rows[0]["Ngay_HHHD"].ToString() != "")
                            tkDuyet.NgayHetHanHopDong = Convert.ToDateTime(dt.Rows[0]["Ngay_HHHD"].ToString());
                        tkDuyet.SoHoaDonThuongMai = Convert.ToString(dt.Rows[0]["So_HDTM"]);
                        if (dt.Rows[0]["Ngay_HDTM"].ToString() != "")
                            tkDuyet.NgayHoaDonThuongMai = Convert.ToDateTime(dt.Rows[0]["Ngay_HDTM".ToString()]);
                        tkDuyet.PTVT_ID = Convert.ToString(dt.Rows[0]["Ma_PTVT"]);
                        tkDuyet.SoHieuPTVT = Convert.ToString(dt.Rows[0]["Ten_PTVT"]);
                        if (dt.Rows[0]["NgayDen"].ToString() != "")
                            tkDuyet.NgayDenPTVT = Convert.ToDateTime(dt.Rows[0]["NgayDen"].ToString());
                        tkDuyet.LoaiVanDon = Convert.ToString(dt.Rows[0]["Van_Don"]);
                        if (dt.Rows[0]["Ngay_VanDon"].ToString() != "")
                            tkDuyet.NgayVanDon = Convert.ToDateTime(dt.Rows[0]["Ngay_VanDon"].ToString());
                        tkDuyet.NuocXK_ID = Convert.ToString(dt.Rows[0]["Nuoc_XK"]);
                        tkDuyet.NuocNK_ID = Convert.ToString(dt.Rows[0]["Nuoc_NK"]);
                        tkDuyet.DiaDiemXepHang = Convert.ToString(dt.Rows[0]["CangNN"]);
                        tkDuyet.DKGH_ID = Convert.ToString(dt.Rows[0]["Ma_GH"]);
                        tkDuyet.CuaKhau_ID = Convert.ToString(dt.Rows[0]["Ma_CK"]);
                        tkDuyet.NguyenTe_ID = Convert.ToString(dt.Rows[0]["Ma_NT"]);
                        tkDuyet.TyGiaTinhThue = Convert.ToDecimal(dt.Rows[0]["TyGia_VND"]);
                        tkDuyet.TyGiaUSD = Convert.ToDecimal(dt.Rows[0]["TyGia_USD"]);
                        tkDuyet.PTTT_ID = Convert.ToString(dt.Rows[0]["Ma_PTTT"]);
                        if (dt.Rows[0]["SoHang"].ToString() != "")
                            tkDuyet.SoHang = Convert.ToInt16(dt.Rows[0]["SoHang"].ToString());
                        if (dt.Rows[0]["So_PLTK"].ToString() != "")
                            tkDuyet.SoLuongPLTK = Convert.ToInt16(dt.Rows[0]["So_PLTK"].ToString());
                        tkDuyet.TenChuHang = Convert.ToString(dt.Rows[0]["TenCH"]);
                        if (dt.Rows[0]["So_Container"].ToString() != "")
                            tkDuyet.SoContainer20 = Convert.ToDecimal(dt.Rows[0]["So_Container"]);
                        if (dt.Rows[0]["So_Container40"].ToString() != "")
                            tkDuyet.SoContainer40 = Convert.ToDecimal(dt.Rows[0]["So_Container40"]);
                        if (dt.Rows[0]["So_Kien"].ToString() != "")
                            tkDuyet.SoKien = Convert.ToDecimal(dt.Rows[0]["So_Kien"]);
                        if (dt.Rows[0]["Tr_Luong"].ToString() != "")
                            tkDuyet.TrongLuong = Convert.ToDecimal(dt.Rows[0]["Tr_Luong"]);
                        if (dt.Rows[0]["TongTGKB"].ToString() != "")
                            tkDuyet.TongTriGiaKhaiBao = Convert.ToDecimal(dt.Rows[0]["TongTGKB"]);
                        if (dt.Rows[0]["TongTGTT"].ToString() != "")
                            tkDuyet.TongTriGiaTinhThue = Convert.ToDecimal(dt.Rows[0]["TongTGTT"]);
                        tkDuyet.LoaiToKhaiGiaCong = Convert.ToString(dt.Rows[0]["LoaiTKGC"]);
                        tkDuyet.MaDoanhNghiep = Convert.ToString(dt.Rows[0]["Ma_DV"]);
                        if (dt.Rows[0]["LePhi_HQ"].ToString() != "")
                            tkDuyet.LePhiHaiQuan = Convert.ToDecimal(dt.Rows[0]["LePhi_HQ"]);
                        if (dt.Rows[0]["Phi_BH"].ToString() != "")
                            tkDuyet.PhiBaoHiem = Convert.ToDecimal(dt.Rows[0]["Phi_BH"]);
                        if (dt.Rows[0]["Phi_VC"].ToString() != "")
                            tkDuyet.PhiVanChuyen = Convert.ToDecimal(dt.Rows[0]["Phi_VC"]);
                        tkDuyet.ThanhLy = "0";
                        tkDuyet.Xuat_NPL_SP = this.LoaiHangHoa;                        
                    }
                        // 3. Xóa danh sách hàng chuyển tiếp cũ.
                        this.LoadHMDCollection();
                        new HangMauDich().DeleteCollection(this.HMDCollection, transaction);
                        this.HMDCollection.Clear();

                        // 4. Cập nhật lại danh sách hàng chuyển tiếp mới.
                        int stt = 1;
                        foreach (DataRow row in dt.Rows)
                        {
                            HangMauDich hmd = new HangMauDich();
                            hmd.TKMD_ID = this.ID;
                            hmd.MaPhu = row["MA_NPL_SP"].ToString();
                            hmd.TenHang = row["Ten_Hang"].ToString();
                            hmd.MaHS = row["Ma_Hang"].ToString();
                            hmd.DVT_ID = row["Ma_DVT"].ToString();
                            hmd.NuocXX_ID = row["Nuoc_XX"].ToString();
                            hmd.SoLuong = Convert.ToDecimal(row["Luong"].ToString());
                            hmd.DonGiaKB = Convert.ToDecimal(row["DGia_KB"]);
                            hmd.DonGiaTT = Convert.ToDecimal(row["DGia_TT"]);
                            hmd.TriGiaKB = Convert.ToDecimal(row["TriGia_KB"]);
                            hmd.TriGiaTT = Convert.ToDecimal(row["TriGia_TT"]);
                            hmd.TriGiaKB_VND = Convert.ToDecimal(row["TGKB_VND"]);
                            hmd.ThueSuatXNK = Convert.ToDecimal(row["TS_XNK"]);
                            hmd.ThueSuatTTDB = Convert.ToDecimal(row["TS_TTDB"]);
                            hmd.ThueSuatGTGT = Convert.ToDecimal(row["TS_VAT"]);
                            hmd.ThueXNK = Convert.ToDecimal(row["Thue_XNK"]);
                            hmd.ThueTTDB = Convert.ToDecimal(row["Thue_TTDB"]);
                            hmd.ThueGTGT = Convert.ToDecimal(row["Thue_VAT"]);
                            hmd.PhuThu = Convert.ToDecimal(row["Phu_Thu"]);
                            hmd.MienThue = Convert.ToByte(row["MienThue"]);
                            hmd.TyLeThuKhac = Convert.ToDecimal(row["TyLe_ThuKhac"]);
                            hmd.TriGiaThuKhac = Convert.ToDecimal(row["TriGia_ThuKhac"]);
                            hmd.SoThuTuHang = stt++;
                            this.HMDCollection.Add(hmd);
                            if (this.TrangThaiXuLy == BLL.TrangThaiXuLy.DA_DUYET)
                            {
                                BLL.SXXK.ToKhai.HangMauDich hmdDuyet = new Company.BLL.SXXK.ToKhai.HangMauDich();
                                hmdDuyet.DonGiaKB = hmd.DonGiaKB;
                                hmdDuyet.DonGiaTT = hmd.DonGiaTT;
                                hmdDuyet.DVT_ID = hmd.DVT_ID;
                                hmdDuyet.MaHaiQuan = this.MaHaiQuan;
                                hmdDuyet.MaHS = hmd.MaHS;
                                hmdDuyet.MaLoaiHinh = this.MaLoaiHinh;
                                hmdDuyet.MaPhu = hmd.MaPhu;
                                hmdDuyet.MienThue = hmd.MienThue;
                                hmdDuyet.NamDangKy=(short)this.NgayDangKy.Year;
                                hmdDuyet.NuocXX_ID = hmd.NuocXX_ID;
                                hmdDuyet.PhuThu = hmd.PhuThu;
                                hmdDuyet.SoLuong = hmd.SoLuong;
                                hmdDuyet.SoThuTuHang = (short)hmd.SoThuTuHang;
                                hmdDuyet.SoToKhai = this.SoToKhai;
                                hmdDuyet.TenHang = hmd.TenHang;
                                hmdDuyet.ThueGTGT = hmdDuyet.ThueGTGT;
                                hmdDuyet.ThueSuatGTGT = hmd.ThueSuatGTGT;
                                hmdDuyet.ThueSuatTTDB = hmd.ThueSuatTTDB;
                                hmdDuyet.ThueSuatXNK = hmd.ThueSuatXNK;
                                hmdDuyet.ThueTTDB = hmd.ThueTTDB;
                                hmdDuyet.ThueXNK = hmd.ThueXNK;
                                hmdDuyet.TriGiaKB = hmd.TriGiaKB;
                                hmdDuyet.TriGiaKB_VND = hmd.TriGiaKB_VND;
                                hmdDuyet.TriGiaThuKhac = hmd.TriGiaThuKhac;
                                hmdDuyet.TriGiaTT = hmd.TriGiaTT;
                                hmdDuyet.TyLeThuKhac = hmd.TyLeThuKhac;
                                tkDuyet.HMDCollection.Add(hmdDuyet);
                            }
                        }
                        string giayTo = "";
                        //xoa giay to cu di
                        ChungTu ctDelete = new ChungTu();
                        ctDelete.Master_ID = this.ID;
                        ctDelete.DeleteCollection(this.ChungTuTKCollection, transaction);
                        this.ChungTuTKCollection.Clear();
                        //cap nhat lai giay to
                        foreach (DataRow row in dtChungTu.Rows)
                        {
                            ChungTu ct = new ChungTu();
                            ct.FileUpLoad = row["FileUpload"].ToString();
                            ct.SoBanChinh = Convert.ToInt16(row["SoBanChinh"].ToString());
                            ct.SoBanSao = Convert.ToInt16(row["SoBanSao"].ToString());
                            ct.STTHang = Convert.ToInt32(row["STTHang"].ToString());
                            ct.Master_ID = this.ID;
                            giayTo = ct.TenChungTu + ";"+giayTo;
                            this.ChungTuTKCollection.Add(ct);
                        }
                        // Cập nhật vào CSDL.
                        this.InsertUpdateFull(transaction);                        
                        // 5. Nếu trạng thái là đã duyệt chính thức thì chuyển danh sách sản phẩm đã duyệt sang phân hệ SXXK.

                        if (this.TrangThaiXuLy == BLL.TrangThaiXuLy.DA_DUYET)
                        {
                            tkDuyet.ChungTu = this.GiayTo;
                            tkDuyet.InsertUpdateTransaction(transaction);
                            BLL.SXXK.ToKhai.HangMauDich hmd = new Company.BLL.SXXK.ToKhai.HangMauDich();
                            hmd.InsertUpdate(tkDuyet.HMDCollection, transaction);
                        }
                        transaction.Commit();
                    }
                    catch (Exception ex)
                    {
                        transaction.Rollback();
                        throw new Exception(ex.Message);
                    }
                    finally
                    {
                        connection.Close();
                    }
                }
            }
            else
            {
                this.Update();
            }
        }



        #endregion

        #region Webservice của hải quan

        private string ConfigPhongBi(int type, int function)
        {
            XmlDocument doc = new XmlDocument();
            doc.Load("TemplateXML\\PhongBi.xml");
            //set thong tin hai quan den
            XmlNode nodeTo = doc.GetElementsByTagName("To")[0];
            XmlNode nodeName = nodeTo.ChildNodes[0];
            nodeName.InnerText = Company.BLL.DuLieuChuan.DonViHaiQuan.GetName(this._MaHaiQuan);
            XmlNode nodeIdentity = nodeTo.ChildNodes[1];
            nodeIdentity.InnerText = this.MaHaiQuan;

            ////set thong so gui di subject
            XmlNode nodeSubject = doc.GetElementsByTagName("Subject")[0];
            XmlNode nodeType = nodeSubject.ChildNodes[0];
            nodeType.InnerText = type.ToString();

            XmlNode nodeFunction = nodeSubject.ChildNodes[1];
            nodeFunction.InnerText = function.ToString();

            XmlNode nodeReference = nodeSubject.ChildNodes[2];
            nodeReference.InnerText = (System.Guid.NewGuid().ToString());

            XmlNode nodeMessageID = doc.GetElementsByTagName("messageId")[0];
            nodeMessageID.InnerText = (System.Guid.NewGuid().ToString());

            return doc.InnerXml;
        }

        #region Khai báo tờ khai nhập
        
        public string WSSendXMLNHAP(string pass)
        {
            XmlDocument doc = new XmlDocument();
            doc.LoadXml(ConfigPhongBi(MessgaseType.ToKhaiNhap, MessgaseFunction.KhaiBao));
            XmlDocument docNPL = new XmlDocument();
            docNPL.LoadXml(ConvertCollectionToXMLNHAP());

            //luu vao string
            XmlNode root = docNPL.GetElementsByTagName("Root")[0].CloneNode(true);
            XmlNode Content = doc.GetElementsByTagName("Content")[0];
            XmlNode root2 = doc.CreateElement("Root");
            root2.InnerXml = root.InnerXml;
            Content.AppendChild(root2);
            WS.KhaiDienTu.KDTService kdt = new Company.BLL.WS.KhaiDienTu.KDTService();
            string kq = kdt.Send(doc.InnerXml, pass);
            return kq;
        }
        private string ConvertCollectionToXMLNHAP()
        {
            //load du lieu
            XmlDocument docNPL = new XmlDocument();            
            docNPL.Load("TemplateXML\\KhaiBaoToKhaiNhap.xml");
            
            //thong tin hai quan nhan
            XmlNode nodeHQNhan = docNPL.GetElementsByTagName("HQ_NHAN")[0];
            nodeHQNhan.Attributes["MA_HQ"].Value = this.MaHaiQuan;
            nodeHQNhan.Attributes["TEN_HQ"].Value = Company.BLL.DuLieuChuan.DonViHaiQuan.GetName(this._MaHaiQuan);

            //thong tin to khai
            XmlNode nodeToKhai = docNPL.GetElementsByTagName("TO_KHAI")[0];
            nodeToKhai.Attributes["MA_LH"].Value = this.MaLoaiHinh;
            nodeToKhai.Attributes["MA_HQ"].Value = this.MaHaiQuan;
            nodeToKhai.Attributes["MA_DV_NK"].Value = this.MaDoanhNghiep;
            nodeToKhai.Attributes["MA_DV_UT"].Value = "";
            nodeToKhai.Attributes["MA_DV_XK"].Value = "";
            nodeToKhai.Attributes["TEN_DV_XK"].Value = this.TenDonViDoiTac;
            nodeToKhai.Attributes["SO_GP"].Value = this.SoGiayPhep;
            nodeToKhai.Attributes["NGAY_GP"].Value = this.NgayGiayPhep.ToString("yyyy-MM-dd");
            nodeToKhai.Attributes["NGAY_HHGP"].Value = this.NgayHetHanGiayPhep.ToString("yyyy-MM-dd");
            nodeToKhai.Attributes["SO_HD"].Value = this.SoHopDong;
            nodeToKhai.Attributes["NGAY_HD"].Value = this.NgayHopDong.ToString("yyyy-MM-dd");
            nodeToKhai.Attributes["NGAY_HHHD"].Value = this.NgayHetHanHopDong.ToString("yyyy-MM-dd");
            nodeToKhai.Attributes["SO_HDTM"].Value = this.SoHoaDonThuongMai;
            nodeToKhai.Attributes["NGAY_HDTM"].Value = this.NgayHoaDonThuongMai.ToString("yyyy-MM-dd");
            nodeToKhai.Attributes["MA_PTVT"].Value = this.PTVT_ID;
            nodeToKhai.Attributes["TEN_PTVT"].Value = Company.BLL.DuLieuChuan.PhuongThucVanTai.getName(this.PTVT_ID);
            nodeToKhai.Attributes["NGAY_DEN"].Value = this.NgayDenPTVT.ToString("yyyy-MM-dd");
            nodeToKhai.Attributes["SO_VANDON"].Value = this.SoVanDon;
            nodeToKhai.Attributes["NGAY_VANDON"].Value = this.NgayVanDon.ToString("yyyy-MM-dd");
            nodeToKhai.Attributes["NUOC_XK"].Value = this.NuocXK_ID;
            nodeToKhai.Attributes["CANG_XUAT"].Value = this.DiaDiemXepHang;
            nodeToKhai.Attributes["MA_CK_NHAP"].Value = this.CuaKhau_ID;
            nodeToKhai.Attributes["MA_DKGH"].Value = this.DKGH_ID;
            nodeToKhai.Attributes["MA_NGTE"].Value = this.NguyenTe_ID;
            nodeToKhai.Attributes["TY_GIA_VND"].Value = this.TyGiaTinhThue.ToString();
            nodeToKhai.Attributes["MA_PTTT"].Value = this.PTTT_ID;
            nodeToKhai.Attributes["SO_PLTK"].Value = this.SoLuongPLTK.ToString();
            nodeToKhai.Attributes["SO_PLTK"].Value = this.SoLuongPLTK.ToString();
            nodeToKhai.Attributes["CHUNGTU_KEM"].Value = "";
            nodeToKhai.Attributes["SO_CONT20"].Value = this.SoContainer20.ToString();
            nodeToKhai.Attributes["SO_CONT40"].Value = this.SoContainer40.ToString();
            nodeToKhai.Attributes["SO_KIEN"].Value = this.SoKien.ToString();
            nodeToKhai.Attributes["TRONG_LUONG"].Value = this.TrongLuong.ToString();
            nodeToKhai.Attributes["PHI_BH"].Value = this.PhiBaoHiem.ToString();
            nodeToKhai.Attributes["PHI_VC"].Value = this.PhiVanChuyen.ToString();
            nodeToKhai.Attributes["LE_PHI_HQ"].Value = this.LePhiHaiQuan.ToString();
            nodeToKhai.Attributes["CHU_HANG"].Value = this.TenChuHang;


            //hang hoa
            if (this.HMDCollection == null || this.HMDCollection.Count == 0)
            {
                HangMauDich hmd = new HangMauDich();
                hmd.TKMD_ID = this.ID;
                this.HMDCollection = hmd.SelectCollectionBy_TKMD_ID();
            }
            XmlNode nodeHang = docNPL.GetElementsByTagName("HANG")[0];         
            foreach (HangMauDich hmd in this.HMDCollection)
            {
                XmlNode node = docNPL.CreateElement("HANG.ITEM");
                XmlAttribute sttAtt = docNPL.CreateAttribute("STT_HANG");
                sttAtt.Value = hmd.SoThuTuHang.ToString();
                node.Attributes.Append(sttAtt);

                XmlAttribute maAtt = docNPL.CreateAttribute("MA_HANG");
                maAtt.Value = hmd.MaPhu;
                node.Attributes.Append(maAtt);

                XmlAttribute MaHSAtt = docNPL.CreateAttribute("MA_HS");
                MaHSAtt.Value = hmd.MaHS;
                node.Attributes.Append(MaHSAtt);

                XmlAttribute TenAtt = docNPL.CreateAttribute("TEN_HANG");
                TenAtt.Value = hmd.TenHang;
                node.Attributes.Append(TenAtt);

                XmlAttribute NuocAtt = docNPL.CreateAttribute("NUOC_XX");
                NuocAtt.Value = hmd.NuocXX_ID;
                node.Attributes.Append(NuocAtt);

                XmlAttribute SoLuongAtt = docNPL.CreateAttribute("LUONG");
                SoLuongAtt.Value = hmd.SoLuong.ToString();
                node.Attributes.Append(SoLuongAtt);

                XmlAttribute Ma_DVTAtt = docNPL.CreateAttribute("MA_DVT");
                Ma_DVTAtt.Value = hmd.DVT_ID;
                node.Attributes.Append(Ma_DVTAtt);

                XmlAttribute DonGiaAtt = docNPL.CreateAttribute("DGIA_NT");
                DonGiaAtt.Value = hmd.DonGiaKB.ToString();
                node.Attributes.Append(DonGiaAtt);

                XmlAttribute TriGiaAtt = docNPL.CreateAttribute("TGIA_NT");
                TriGiaAtt.Value = hmd.TriGiaKB.ToString(); ;
                node.Attributes.Append(TriGiaAtt);

                XmlAttribute DonGiaVNDAtt = docNPL.CreateAttribute("DGIA_TT_VND");
                DonGiaVNDAtt.Value = hmd.DonGiaTT.ToString(); ;
                node.Attributes.Append(DonGiaVNDAtt);

                XmlAttribute TriGiaVNDAtt = docNPL.CreateAttribute("TGIA_TT_VND");
                TriGiaVNDAtt.Value = hmd.TriGiaTT.ToString(); ;
                node.Attributes.Append(TriGiaVNDAtt);

                XmlAttribute TS_XNKAtt = docNPL.CreateAttribute("TS_XNK");
                TS_XNKAtt.Value = hmd.ThueSuatXNK.ToString();
                node.Attributes.Append(TS_XNKAtt);

                XmlAttribute TS_TTDBAtt = docNPL.CreateAttribute("TS_TTDB");
                TS_TTDBAtt.Value = hmd.ThueSuatTTDB.ToString();
                node.Attributes.Append(TS_TTDBAtt);

                XmlAttribute TS_VSTAtt = docNPL.CreateAttribute("TS_VAT");
                TS_VSTAtt.Value = hmd.ThueGTGT.ToString();
                node.Attributes.Append(TS_VSTAtt);

                XmlAttribute TL_PhuThuAtt = docNPL.CreateAttribute("TL_PHU_THU");
                TL_PhuThuAtt.Value = hmd.TyLeThuKhac.ToString();
                node.Attributes.Append(TL_PhuThuAtt);

                XmlAttribute ThueXNKAtt = docNPL.CreateAttribute("THUE_XNK");
                ThueXNKAtt.Value = hmd.ThueXNK.ToString();
                node.Attributes.Append(ThueXNKAtt);

                XmlAttribute ThueTTDBAtt = docNPL.CreateAttribute("THUE_TTDB");
                ThueTTDBAtt.Value = hmd.ThueTTDB.ToString();
                node.Attributes.Append(ThueTTDBAtt);

                XmlAttribute ThueVATAtt = docNPL.CreateAttribute("THUE_VAT");
                ThueVATAtt.Value = hmd.ThueGTGT.ToString();
                node.Attributes.Append(ThueVATAtt);

                XmlAttribute TPhuThuAtt = docNPL.CreateAttribute("PHU_THU");
                TPhuThuAtt.Value = hmd.PhuThu.ToString();
                node.Attributes.Append(TPhuThuAtt);

                nodeHang.AppendChild(node);
            }
            return docNPL.InnerXml;
        }

        #endregion Khai báo tờ khai nhập

        #region Huy khai báo danh mục nguyên phụ liệu

        public string WSCancelXML(string pass,int loaiTK)
        {
            XmlDocument doc = new XmlDocument();
            doc.LoadXml(ConfigPhongBi(loaiTK, MessgaseFunction.HuyKhaiBao));

            XmlDocument docNPL = new XmlDocument();
            docNPL.Load("TemplateXML\\HuyKhaiBaoToKhaiNhap.xml");

            XmlNode nodeHQNhan = docNPL.GetElementsByTagName("HQ_NHAN")[0];
            nodeHQNhan.Attributes["MA_HQ"].Value = this.MaHaiQuan;
            nodeHQNhan.Attributes["TEN_HQ"].Value = Company.BLL.DuLieuChuan.DonViHaiQuan.GetName(this._MaHaiQuan);

            XmlNode nodeDuLieu = docNPL.GetElementsByTagName("DU_LIEU")[0];
            nodeDuLieu.Attributes["SO_TN"].Value = this.SoTiepNhan.ToString();
            nodeDuLieu.Attributes["NAM_TN"].Value = this.NgayTiepNhan.Year.ToString();

            XmlNode root = docNPL.GetElementsByTagName("Root")[0].CloneNode(true);
            XmlNode Content = doc.GetElementsByTagName("Content")[0];
            XmlNode root2 = doc.CreateElement("Root");

            root2.InnerXml = root.InnerXml;
            Content.AppendChild(root2);
            WS.KhaiDienTu.KDTService kdt = new Company.BLL.WS.KhaiDienTu.KDTService();
            string kq = kdt.Send(doc.InnerXml, pass);
            return kq;

        }

        #endregion Huy khai báo danh mục nguyên phụ liệu

        #region Lấy phản hồi

        public string WSRequestXML(string pass,int loaiTK)
        {
            XmlDocument doc = new XmlDocument();
            doc.LoadXml(ConfigPhongBi(loaiTK, MessgaseFunction.LayPhanHoi));

            XmlDocument docNPL = new XmlDocument();
            docNPL.Load("TemplateXML\\LayPhanHoiToKhai.xml");

            XmlNode nodeHQNhan = docNPL.GetElementsByTagName("HQ_NHAN")[0];
            nodeHQNhan.Attributes["MA_HQ"].Value = this.MaHaiQuan;
            nodeHQNhan.Attributes["TEN_HQ"].Value = Company.BLL.DuLieuChuan.DonViHaiQuan.GetName(this._MaHaiQuan);

            XmlNode nodeDuLieu = docNPL.GetElementsByTagName("DU_LIEU")[0];
            nodeDuLieu.Attributes["SO_TN"].Value = this.SoTiepNhan.ToString();
            nodeDuLieu.Attributes["NAM_TN"].Value = this.NgayTiepNhan.Year.ToString();

            XmlNode root = docNPL.GetElementsByTagName("Root")[0].CloneNode(true);
            XmlNode Content = doc.GetElementsByTagName("Content")[0];
            XmlNode root2 = doc.CreateElement("Root");

            root2.InnerXml = root.InnerXml;
            Content.AppendChild(root2);

            WS.KhaiDienTu.KDTService kdt = new Company.BLL.WS.KhaiDienTu.KDTService();
            string kq = kdt.Send(doc.InnerXml, pass);
            return kq;

        }

        #endregion Lấy phản hồi

        #region Khai báo tờ khai xuất

        public string WSSendXMLXuat(string pass)
        {
            XmlDocument doc = new XmlDocument();
            doc.LoadXml(ConfigPhongBi(MessgaseType.ToKhaiXuat, MessgaseFunction.KhaiBao));
            XmlDocument docNPL = new XmlDocument();
            docNPL.LoadXml(ConvertCollectionToXMLXUAT());

            //luu vao string
            XmlNode root = docNPL.GetElementsByTagName("Root")[0].CloneNode(true);
            XmlNode Content = doc.GetElementsByTagName("Content")[0];
            XmlNode root2 = doc.CreateElement("Root");
            root2.InnerXml = root.InnerXml;
            Content.AppendChild(root2);
            WS.KhaiDienTu.KDTService kdt = new Company.BLL.WS.KhaiDienTu.KDTService();
            string kq = kdt.Send(doc.InnerXml, pass);
            return kq;
        }
        private string ConvertCollectionToXMLXUAT()
        {
            //load du lieu
            XmlDocument docNPL = new XmlDocument();
            docNPL.Load("TemplateXML\\KhaiBaoToKhaiXuat.xml");

            //thong tin hai quan nhan
            XmlNode nodeHQNhan = docNPL.GetElementsByTagName("HQ_NHAN")[0];
            nodeHQNhan.Attributes["MA_HQ"].Value = this.MaHaiQuan;
            nodeHQNhan.Attributes["TEN_HQ"].Value = Company.BLL.DuLieuChuan.DonViHaiQuan.GetName(this._MaHaiQuan);

            //thong tin to khai
            XmlNode nodeToKhai = docNPL.GetElementsByTagName("TO_KHAI")[0];
            nodeToKhai.Attributes["XUAT_NPL_SP"].Value = this.LoaiHangHoa;
            nodeToKhai.Attributes["MA_LH"].Value = this.MaLoaiHinh;
            nodeToKhai.Attributes["MA_HQ"].Value = this.MaHaiQuan;
            nodeToKhai.Attributes["MA_DV_XK"].Value = this.MaDoanhNghiep;
            nodeToKhai.Attributes["MA_DV_UT"].Value = "";
            nodeToKhai.Attributes["MA_DV_NK"].Value = "";
            nodeToKhai.Attributes["TEN_DV_NK"].Value = this.TenDonViDoiTac;
            nodeToKhai.Attributes["SO_GP"].Value = this.SoGiayPhep;
            nodeToKhai.Attributes["NGAY_GP"].Value = this.NgayGiayPhep.ToString("yyyy-MM-dd");
            nodeToKhai.Attributes["NGAY_HHGP"].Value = this.NgayHetHanGiayPhep.ToString("yyyy-MM-dd");
            nodeToKhai.Attributes["SO_HD"].Value = this.SoHopDong;
            nodeToKhai.Attributes["NGAY_HD"].Value = this.NgayHopDong.ToString("yyyy-MM-dd");
            nodeToKhai.Attributes["NGAY_HHHD"].Value = this.NgayHetHanHopDong.ToString("yyyy-MM-dd");
            nodeToKhai.Attributes["SO_HDTM"].Value = this.SoHoaDonThuongMai;
            nodeToKhai.Attributes["NGAY_HDTM"].Value = this.NgayHoaDonThuongMai.ToString("yyyy-MM-dd");
            nodeToKhai.Attributes["MA_PTVT"].Value = this.PTVT_ID;
            nodeToKhai.Attributes["TEN_PTVT"].Value = Company.BLL.DuLieuChuan.PhuongThucVanTai.getName(this.PTVT_ID);
            nodeToKhai.Attributes["NGAY_DEN"].Value = this.NgayDenPTVT.ToString("yyyy-MM-dd");
            nodeToKhai.Attributes["SO_VANDON"].Value = this.SoVanDon;
            nodeToKhai.Attributes["NGAY_VANDON"].Value = this.NgayVanDon.ToString("yyyy-MM-dd");
            nodeToKhai.Attributes["NUOC_XK"].Value = this.NuocXK_ID;
            nodeToKhai.Attributes["CANG_XUAT"].Value = this.DiaDiemXepHang;
            nodeToKhai.Attributes["MA_CK_NHAP"].Value = this.CuaKhau_ID;
            nodeToKhai.Attributes["MA_DKGH"].Value = this.DKGH_ID;
            nodeToKhai.Attributes["MA_NGTE"].Value = this.NguyenTe_ID;
            nodeToKhai.Attributes["TY_GIA_VND"].Value = this.TyGiaTinhThue.ToString();
            nodeToKhai.Attributes["MA_PTTT"].Value = this.PTTT_ID;
            nodeToKhai.Attributes["SO_PLTK"].Value = this.SoLuongPLTK.ToString();
            nodeToKhai.Attributes["SO_PLTK"].Value = this.SoLuongPLTK.ToString();
            nodeToKhai.Attributes["CHUNGTU_KEM"].Value = "";
            nodeToKhai.Attributes["SO_CONT20"].Value = this.SoContainer20.ToString();
            nodeToKhai.Attributes["SO_CONT40"].Value = this.SoContainer40.ToString();
            nodeToKhai.Attributes["SO_KIEN"].Value = this.SoKien.ToString();
            nodeToKhai.Attributes["TRONG_LUONG"].Value = this.TrongLuong.ToString();
            nodeToKhai.Attributes["PHI_BH"].Value = this.PhiBaoHiem.ToString();
            nodeToKhai.Attributes["PHI_VC"].Value = this.PhiVanChuyen.ToString();
            nodeToKhai.Attributes["LE_PHI_HQ"].Value = this.LePhiHaiQuan.ToString();
            nodeToKhai.Attributes["CHU_HANG"].Value = this.TenChuHang;


            //hang hoa
            if (this.HMDCollection == null || this.HMDCollection.Count == 0)
            {
                HangMauDich hmd = new HangMauDich();
                hmd.TKMD_ID = this.ID;
                this.HMDCollection = hmd.SelectCollectionBy_TKMD_ID();
            }
            XmlNode nodeHang = docNPL.GetElementsByTagName("HANG")[0];
            foreach (HangMauDich hmd in this.HMDCollection)
            {
                XmlNode node = docNPL.CreateElement("HANG.ITEM");
                XmlAttribute sttAtt = docNPL.CreateAttribute("STT_HANG");
                sttAtt.Value = hmd.SoThuTuHang.ToString();
                node.Attributes.Append(sttAtt);

                XmlAttribute maAtt = docNPL.CreateAttribute("MA_HANG");
                maAtt.Value = hmd.MaPhu;
                node.Attributes.Append(maAtt);

                XmlAttribute MaHSAtt = docNPL.CreateAttribute("MA_HS");
                MaHSAtt.Value = hmd.MaHS;
                node.Attributes.Append(MaHSAtt);

                XmlAttribute TenAtt = docNPL.CreateAttribute("TEN_HANG");
                TenAtt.Value = hmd.TenHang;
                node.Attributes.Append(TenAtt);

                XmlAttribute NuocAtt = docNPL.CreateAttribute("NUOC_XX");
                NuocAtt.Value = hmd.NuocXX_ID;
                node.Attributes.Append(NuocAtt);

                XmlAttribute SoLuongAtt = docNPL.CreateAttribute("LUONG");
                SoLuongAtt.Value = hmd.SoLuong.ToString();
                node.Attributes.Append(SoLuongAtt);

                XmlAttribute Ma_DVTAtt = docNPL.CreateAttribute("MA_DVT");
                Ma_DVTAtt.Value = hmd.DVT_ID;
                node.Attributes.Append(Ma_DVTAtt);

                XmlAttribute DonGiaAtt = docNPL.CreateAttribute("DGIA_NT");
                DonGiaAtt.Value = hmd.DonGiaKB.ToString();
                node.Attributes.Append(DonGiaAtt);

                XmlAttribute TriGiaAtt = docNPL.CreateAttribute("TGIA_NT");
                TriGiaAtt.Value = hmd.TriGiaKB.ToString(); ;
                node.Attributes.Append(TriGiaAtt);

                XmlAttribute DonGiaVNDAtt = docNPL.CreateAttribute("DGIA_TT_VND");
                DonGiaVNDAtt.Value = hmd.DonGiaTT.ToString(); ;
                node.Attributes.Append(DonGiaVNDAtt);

                XmlAttribute TriGiaVNDAtt = docNPL.CreateAttribute("TGIA_TT_VND");
                TriGiaVNDAtt.Value = hmd.TriGiaTT.ToString(); ;
                node.Attributes.Append(TriGiaVNDAtt);

                XmlAttribute TS_XNKAtt = docNPL.CreateAttribute("TS_XNK");
                TS_XNKAtt.Value = hmd.ThueSuatXNK.ToString();
                node.Attributes.Append(TS_XNKAtt);

                XmlAttribute TS_TTDBAtt = docNPL.CreateAttribute("TS_TTDB");
                TS_TTDBAtt.Value = hmd.ThueSuatTTDB.ToString();
                node.Attributes.Append(TS_TTDBAtt);

                XmlAttribute TS_VSTAtt = docNPL.CreateAttribute("TS_VAT");
                TS_VSTAtt.Value = hmd.ThueGTGT.ToString();
                node.Attributes.Append(TS_VSTAtt);

                XmlAttribute TL_PhuThuAtt = docNPL.CreateAttribute("TL_PHU_THU");
                TL_PhuThuAtt.Value = hmd.TyLeThuKhac.ToString();
                node.Attributes.Append(TL_PhuThuAtt);

                XmlAttribute ThueXNKAtt = docNPL.CreateAttribute("THUE_XNK");
                ThueXNKAtt.Value = hmd.ThueXNK.ToString();
                node.Attributes.Append(ThueXNKAtt);

                XmlAttribute ThueTTDBAtt = docNPL.CreateAttribute("THUE_TTDB");
                ThueTTDBAtt.Value = hmd.ThueTTDB.ToString();
                node.Attributes.Append(ThueTTDBAtt);

                XmlAttribute ThueVATAtt = docNPL.CreateAttribute("THUE_VAT");
                ThueVATAtt.Value = hmd.ThueGTGT.ToString();
                node.Attributes.Append(ThueVATAtt);

                XmlAttribute TPhuThuAtt = docNPL.CreateAttribute("PHU_THU");
                TPhuThuAtt.Value = hmd.PhuThu.ToString();
                node.Attributes.Append(TPhuThuAtt);

                nodeHang.AppendChild(node);
            }
            return docNPL.InnerXml;
        }
        #endregion Khai báo tờ khai xuất

        #endregion Webservice của hải quan

        #region Webservice của FPT

        private XmlNode ConvertToKhaiToXMLNHAP(HangMauDichCollection collection,XmlDocument docNPL)
        {
            //load du lieu
            //XmlDocument docNPL = new XmlDocument();
            //docNPL.Load(@"FPTService\SXXK_TKN_CAPNHAT.xml");
            XmlNode nodeToKhai = docNPL.SelectSingleNode("Root/SXXK/DU_LIEU/TO_KHAI_NK/TO_KHAI");
            nodeToKhai.Attributes["MA_LH"].Value = this.MaLoaiHinh;
            nodeToKhai.Attributes["MA_HQ"].Value = this.MaHaiQuan;
            nodeToKhai.Attributes["MA_DV_NK"].Value = this.MaDoanhNghiep;
            nodeToKhai.Attributes["MA_DV_UT"].Value = "";
            nodeToKhai.Attributes["MA_DV_XK"].Value = "";
            nodeToKhai.Attributes["TEN_DV_XK"].Value = this.TenDonViDoiTac;
            nodeToKhai.Attributes["SO_GP"].Value = this.SoGiayPhep;
            nodeToKhai.Attributes["NGAY_GP"].Value = this.NgayGiayPhep.ToString("yyyy-MM-dd");
            nodeToKhai.Attributes["NGAY_HHGP"].Value = this.NgayHetHanGiayPhep.ToString("yyyy-MM-dd");
            nodeToKhai.Attributes["SO_HD"].Value = this.SoHopDong;
            nodeToKhai.Attributes["NGAY_HD"].Value = this.NgayHopDong.ToString("yyyy-MM-dd");
            nodeToKhai.Attributes["NGAY_HHHD"].Value = this.NgayHetHanHopDong.ToString("yyyy-MM-dd");
            nodeToKhai.Attributes["SO_HDTM"].Value = this.SoHoaDonThuongMai;
            nodeToKhai.Attributes["NGAY_HDTM"].Value = this.NgayHoaDonThuongMai.ToString("yyyy-MM-dd");
            nodeToKhai.Attributes["MA_PTVT"].Value = this.PTVT_ID;
            nodeToKhai.Attributes["TEN_PTVT"].Value = Company.BLL.DuLieuChuan.PhuongThucVanTai.getName(this.PTVT_ID);
            nodeToKhai.Attributes["NGAY_DEN"].Value = this.NgayDenPTVT.ToString("yyyy-MM-dd");
            nodeToKhai.Attributes["SO_VANDON"].Value = this.SoVanDon;
            nodeToKhai.Attributes["NGAY_VANDON"].Value = this.NgayVanDon.ToString("yyyy-MM-dd");
            nodeToKhai.Attributes["NUOC_XK"].Value = this.NuocXK_ID;
            nodeToKhai.Attributes["CANG_XUAT"].Value = this.DiaDiemXepHang;
            nodeToKhai.Attributes["MA_CK_NHAP"].Value = this.CuaKhau_ID;
            nodeToKhai.Attributes["MA_DKGH"].Value = this.DKGH_ID;
            nodeToKhai.Attributes["MA_NGTE"].Value = this.NguyenTe_ID;
            nodeToKhai.Attributes["TY_GIA_VND"].Value = this.TyGiaTinhThue.ToString();
            nodeToKhai.Attributes["MA_PTTT"].Value = this.PTTT_ID;
            nodeToKhai.Attributes["SO_PLTK"].Value = this.SoLuongPLTK.ToString();                        
          

            //hang hoa

            foreach (HangMauDich hmd in collection)
            {
                XmlNode node = docNPL.CreateElement("HANG");
                XmlAttribute sttAtt = docNPL.CreateAttribute("STT_HANG");
                sttAtt.Value = hmd.SoThuTuHang.ToString();
                node.Attributes.Append(sttAtt);

                XmlAttribute maAtt = docNPL.CreateAttribute("MA_HANG");
                maAtt.Value = hmd.MaPhu;
                node.Attributes.Append(maAtt);

                XmlAttribute MaHSAtt = docNPL.CreateAttribute("MA_HS");
                MaHSAtt.Value = hmd.MaHS;
                node.Attributes.Append(MaHSAtt);

                XmlAttribute TenAtt = docNPL.CreateAttribute("TEN_HANG");
                TenAtt.Value = hmd.TenHang;
                node.Attributes.Append(TenAtt);

                XmlAttribute NuocAtt = docNPL.CreateAttribute("NUOC_XX");
                NuocAtt.Value = hmd.NuocXX_ID;
                node.Attributes.Append(NuocAtt);

                XmlAttribute SoLuongAtt = docNPL.CreateAttribute("LUONG");
                SoLuongAtt.Value = hmd.SoLuong.ToString();
                node.Attributes.Append(SoLuongAtt);

                XmlAttribute Ma_DVTAtt = docNPL.CreateAttribute("MA_DVT");
                Ma_DVTAtt.Value = hmd.DVT_ID;
                node.Attributes.Append(Ma_DVTAtt);

                XmlAttribute DonGiaAtt = docNPL.CreateAttribute("DGIA_NT");
                DonGiaAtt.Value = hmd.DonGiaKB.ToString();
                node.Attributes.Append(DonGiaAtt);

                XmlAttribute TriGiaAtt = docNPL.CreateAttribute("TGIA_NT");
                TriGiaAtt.Value = hmd.TriGiaKB.ToString(); ;
                node.Attributes.Append(TriGiaAtt);

                XmlAttribute DonGiaVNDAtt = docNPL.CreateAttribute("DGIA_TT_VND");
                DonGiaVNDAtt.Value = hmd.DonGiaTT.ToString(); ;
                node.Attributes.Append(DonGiaVNDAtt);

                XmlAttribute TriGiaVNDAtt = docNPL.CreateAttribute("TGIA_TT_VND");
                TriGiaVNDAtt.Value = hmd.TriGiaTT.ToString(); ;
                node.Attributes.Append(TriGiaVNDAtt);

                XmlAttribute TS_XNKAtt = docNPL.CreateAttribute("TS_XNK");
                TS_XNKAtt.Value = hmd.ThueSuatXNK.ToString();
                node.Attributes.Append(TS_XNKAtt);

                XmlAttribute TS_TTDBAtt = docNPL.CreateAttribute("TS_TTDB");
                TS_TTDBAtt.Value = hmd.ThueSuatTTDB.ToString();
                node.Attributes.Append(TS_TTDBAtt);

                XmlAttribute TS_VSTAtt = docNPL.CreateAttribute("TS_VAT");
                TS_VSTAtt.Value = hmd.ThueGTGT.ToString();
                node.Attributes.Append(TS_VSTAtt);

                XmlAttribute TL_PhuThuAtt = docNPL.CreateAttribute("TL_PHU_THU");
                TL_PhuThuAtt.Value = hmd.TyLeThuKhac.ToString();
                node.Attributes.Append(TL_PhuThuAtt);

                XmlAttribute ThueXNKAtt = docNPL.CreateAttribute("THUE_XNK");
                ThueXNKAtt.Value = hmd.ThueXNK.ToString();
                node.Attributes.Append(ThueXNKAtt);

                XmlAttribute ThueTTDBAtt = docNPL.CreateAttribute("THUE_TTDB");
                ThueTTDBAtt.Value = hmd.ThueTTDB.ToString();
                node.Attributes.Append(ThueTTDBAtt);

                XmlAttribute ThueVATAtt = docNPL.CreateAttribute("THUE_VAT");
                ThueVATAtt.Value = hmd.ThueGTGT.ToString();
                node.Attributes.Append(ThueVATAtt);

                XmlAttribute TPhuThuAtt = docNPL.CreateAttribute("PHU_THU");
                TPhuThuAtt.Value = hmd.PhuThu.ToString();
                node.Attributes.Append(TPhuThuAtt);

                nodeToKhai.AppendChild(node);
            }
            return nodeToKhai;
        }

        private XmlNode ConvertToKhaiToXMLXUAT(HangMauDichCollection collection)
        {
            //load du lieu
            XmlDocument docNPL = new XmlDocument();
            docNPL.Load(@"FPTService\SXXK_TKX_CAPNHAT.xml");
          
            XmlNode nodeToKhai = docNPL.SelectSingleNode("Root/SXXK/DU_LIEU/TO_KHAI_XK/TO_KHAI");          
            //thong tin to khai
            
            nodeToKhai.Attributes["XUAT_NPL_SP"].Value = this.LoaiHangHoa;
            nodeToKhai.Attributes["MA_LH"].Value = this.MaLoaiHinh;
            nodeToKhai.Attributes["MA_HQ"].Value = this.MaHaiQuan;
            nodeToKhai.Attributes["MA_DV_XK"].Value = this.MaDoanhNghiep;
            nodeToKhai.Attributes["MA_DV_UT"].Value = "";
            nodeToKhai.Attributes["MA_DV_NK"].Value = "";
            nodeToKhai.Attributes["TEN_DV_NK"].Value = this.TenDonViDoiTac;
            nodeToKhai.Attributes["SO_GP"].Value = this.SoGiayPhep;
            nodeToKhai.Attributes["NGAY_GP"].Value = this.NgayGiayPhep.ToString("yyyy-MM-dd");
            nodeToKhai.Attributes["NGAY_HHGP"].Value = this.NgayHetHanGiayPhep.ToString("yyyy-MM-dd");
            nodeToKhai.Attributes["SO_HD"].Value = this.SoHopDong;
            nodeToKhai.Attributes["NGAY_HD"].Value = this.NgayHopDong.ToString("yyyy-MM-dd");
            nodeToKhai.Attributes["NGAY_HHHD"].Value = this.NgayHetHanHopDong.ToString("yyyy-MM-dd");
            nodeToKhai.Attributes["SO_HDTM"].Value = this.SoHoaDonThuongMai;
            nodeToKhai.Attributes["NGAY_HDTM"].Value = this.NgayHoaDonThuongMai.ToString("yyyy-MM-dd");
            nodeToKhai.Attributes["MA_PTVT"].Value = this.PTVT_ID;
            nodeToKhai.Attributes["TEN_PTVT"].Value = Company.BLL.DuLieuChuan.PhuongThucVanTai.getName(this.PTVT_ID);
            nodeToKhai.Attributes["NGAY_DEN"].Value = this.NgayDenPTVT.ToString("yyyy-MM-dd");
            nodeToKhai.Attributes["SO_VANDON"].Value = this.SoVanDon;
            nodeToKhai.Attributes["NGAY_VANDON"].Value = this.NgayVanDon.ToString("yyyy-MM-dd");
            nodeToKhai.Attributes["NUOC_XK"].Value = this.NuocXK_ID;
            nodeToKhai.Attributes["CANG_XUAT"].Value = this.DiaDiemXepHang;
            nodeToKhai.Attributes["MA_CK_NHAP"].Value = this.CuaKhau_ID;
            nodeToKhai.Attributes["MA_DKGH"].Value = this.DKGH_ID;
            nodeToKhai.Attributes["MA_NGTE"].Value = this.NguyenTe_ID;
            nodeToKhai.Attributes["TY_GIA_VND"].Value = this.TyGiaTinhThue.ToString();
            nodeToKhai.Attributes["MA_PTTT"].Value = this.PTTT_ID;
            nodeToKhai.Attributes["SO_PLTK"].Value = this.SoLuongPLTK.ToString();
           
            //hang hoa

            foreach (HangMauDich hmd in collection)
            {
                XmlNode node = docNPL.CreateElement("HANG");
                XmlAttribute sttAtt = docNPL.CreateAttribute("STT_HANG");
                sttAtt.Value = hmd.SoThuTuHang.ToString();
                node.Attributes.Append(sttAtt);

                XmlAttribute maAtt = docNPL.CreateAttribute("MA_HANG");
                maAtt.Value = hmd.MaPhu;
                node.Attributes.Append(maAtt);

                XmlAttribute MaHSAtt = docNPL.CreateAttribute("MA_HS");
                MaHSAtt.Value = hmd.MaHS;
                node.Attributes.Append(MaHSAtt);

                XmlAttribute TenAtt = docNPL.CreateAttribute("TEN_HANG");
                TenAtt.Value = hmd.TenHang;
                node.Attributes.Append(TenAtt);

                XmlAttribute NuocAtt = docNPL.CreateAttribute("NUOC_XX");
                NuocAtt.Value = hmd.NuocXX_ID;
                node.Attributes.Append(NuocAtt);

                XmlAttribute SoLuongAtt = docNPL.CreateAttribute("LUONG");
                SoLuongAtt.Value = hmd.SoLuong.ToString();
                node.Attributes.Append(SoLuongAtt);

                XmlAttribute Ma_DVTAtt = docNPL.CreateAttribute("MA_DVT");
                Ma_DVTAtt.Value = hmd.DVT_ID;
                node.Attributes.Append(Ma_DVTAtt);

                XmlAttribute DonGiaAtt = docNPL.CreateAttribute("DGIA_NT");
                DonGiaAtt.Value = hmd.DonGiaKB.ToString();
                node.Attributes.Append(DonGiaAtt);

                XmlAttribute TriGiaAtt = docNPL.CreateAttribute("TGIA_NT");
                TriGiaAtt.Value = hmd.TriGiaKB.ToString(); ;
                node.Attributes.Append(TriGiaAtt);

                XmlAttribute DonGiaVNDAtt = docNPL.CreateAttribute("DGIA_TT_VND");
                DonGiaVNDAtt.Value = hmd.DonGiaTT.ToString(); ;
                node.Attributes.Append(DonGiaVNDAtt);

                XmlAttribute TriGiaVNDAtt = docNPL.CreateAttribute("TGIA_TT_VND");
                TriGiaVNDAtt.Value = hmd.TriGiaTT.ToString(); ;
                node.Attributes.Append(TriGiaVNDAtt);

                XmlAttribute TS_XNKAtt = docNPL.CreateAttribute("TS_XNK");
                TS_XNKAtt.Value = hmd.ThueSuatXNK.ToString();
                node.Attributes.Append(TS_XNKAtt);

                XmlAttribute TS_TTDBAtt = docNPL.CreateAttribute("TS_TTDB");
                TS_TTDBAtt.Value = hmd.ThueSuatTTDB.ToString();
                node.Attributes.Append(TS_TTDBAtt);

                XmlAttribute TS_VSTAtt = docNPL.CreateAttribute("TS_VAT");
                TS_VSTAtt.Value = hmd.ThueGTGT.ToString();
                node.Attributes.Append(TS_VSTAtt);

                XmlAttribute TL_PhuThuAtt = docNPL.CreateAttribute("TL_PHU_THU");
                TL_PhuThuAtt.Value = hmd.TyLeThuKhac.ToString();
                node.Attributes.Append(TL_PhuThuAtt);

                XmlAttribute ThueXNKAtt = docNPL.CreateAttribute("THUE_XNK");
                ThueXNKAtt.Value = hmd.ThueXNK.ToString();
                node.Attributes.Append(ThueXNKAtt);

                XmlAttribute ThueTTDBAtt = docNPL.CreateAttribute("THUE_TTDB");
                ThueTTDBAtt.Value = hmd.ThueTTDB.ToString();
                node.Attributes.Append(ThueTTDBAtt);

                XmlAttribute ThueVATAtt = docNPL.CreateAttribute("THUE_VAT");
                ThueVATAtt.Value = hmd.ThueGTGT.ToString();
                node.Attributes.Append(ThueVATAtt);

                XmlAttribute TPhuThuAtt = docNPL.CreateAttribute("PHU_THU");
                TPhuThuAtt.Value = hmd.PhuThu.ToString();
                node.Attributes.Append(TPhuThuAtt);

                nodeToKhai.AppendChild(node);
            }
            return nodeToKhai;
        }
        #region Khai báo tờ khai nhập

        //cap nhat mot to khai
        public string WSUpdateXMLNHAPFPT(string pass,HangMauDichCollection collection)
        {
            XmlDocument doc = new XmlDocument();
            doc.Load(@"FPTService\SXXK_TKN_CAPNHAT.xml");
         
            //luu vao string
            XmlNode hqNhan = doc.SelectSingleNode("Root/SXXK/THONG_TIN/HQ_NHAN");
            hqNhan.Attributes["MA_HQ"].Value = this.MaHaiQuan;
            hqNhan.Attributes["TEN_HQ"].Value =Company.BLL.DuLieuChuan.DonViHaiQuan.GetName(this.MaHaiQuan);
            XmlNode data = doc.SelectSingleNode("Root/SXXK/DU_LIEU");
            data.Attributes["SO_DK"].Value = this.SoTiepNhan.ToString();
            //thong tin to khai
            //XmlNode tokhai = doc.SelectSingleNode("Root/SXXK/DU_LIEU/TO_KHAI_NK/TO_KHAI");          
            //doc.RemoveChild(tokhai);
            //XmlNode tokhaiList = doc.SelectSingleNode("Root/SXXK/DU_LIEU/TO_KHAI_NK");
            //tokhaiList.AppendChild(ConvertToKhaiToXMLNHAP(collection),doc);
            ConvertToKhaiToXMLNHAP(collection,doc);
            
            WS.FPT.SXXK.SXXKService service = new Company.BLL.WS.FPT.SXXK.SXXKService();
            string kq = service.DataProcess(doc.InnerXml, "ecusxgeiyklgA\\SmqUK@rbiqDyAgbXBIlBTUTL;", pass);
            return kq;

        }

        //dang ky
        public string WSSendXMLNHAPFPT(string pass)
        {
            XmlDocument doc = new XmlDocument();
            doc.Load(@"FPTService\SXXK_TKN_DK.xml");

            //luu vao string
            XmlNode hqNhan = doc.SelectSingleNode("Root/SXXK/THONG_TIN/HQ_NHAN");
            hqNhan.Attributes["MA_HQ"].Value = this.MaHaiQuan;
            hqNhan.Attributes["TEN_HQ"].Value = Company.BLL.DuLieuChuan.DonViHaiQuan.GetName(this.MaHaiQuan);
            //thong tin to khai                        
            XmlNode dv = doc.SelectSingleNode("Root/SXXK/THONG_TIN/DON_VI_GUI");
            dv.Attributes["MA_DV"].Value = this.MaDoanhNghiep;
            dv.Attributes["TEN_DV"].Value = "";
           
            //XmlNode tokhaiList = doc.SelectSingleNode("Root/SXXK/DU_LIEU/TO_KHAI_NK");
            //XmlNode tokhai = doc.SelectSingleNode("Root/SXXK/DU_LIEU/TO_KHAI_NK/TO_KHAI");
            //tokhaiList.RemoveChild(tokhai);
            ConvertToKhaiToXMLNHAP(this.HMDCollection,doc);
            //tokhaiList.AppendChild(xmlNew);

            WS.FPT.SXXK.SXXKService service = new Company.BLL.WS.FPT.SXXK.SXXKService();
            string kq = service.DataProcess(doc.InnerXml, "ecusxgeiyklgA\\SmqUK@rbiqDyAgbXBIlBTUTL;", pass);
            return kq;

        }

        //dang ky nhieu to khai
        public static string WSSendXMLNHAPFPT(string pass,string[] list)
        {
            XmlDocument doc = new XmlDocument();
            doc.Load(@"FPTService\SXXK_TKN_DK.xml");

            //luu vao string
            ToKhaiMauDich tkmd1 = new ToKhaiMauDich();
            tkmd1.ID = Convert.ToInt64(list[0]);
            tkmd1.Load();
            XmlNode hqNhan = doc.SelectSingleNode("Root/SXXK/THONG_TIN/HQ_NHAN");
            hqNhan.Attributes["MA_HQ"].Value = tkmd1.MaHaiQuan;
            hqNhan.Attributes["TEN_HQ"].Value = Company.BLL.DuLieuChuan.DonViHaiQuan.GetName(tkmd1.MaHaiQuan);
            //thong tin to khai
            XmlNode tokhai = doc.SelectSingleNode("Root/SXXK/DU_LIEU/TO_KHAI_NK/TO_KHAI");
            doc.RemoveChild(tokhai);
            XmlNode tokhaiList = doc.SelectSingleNode(@"Root/SXXK/DU_LIEU/TO_KHAI_NK");
            for (int i = 0; i < list.Length; ++i)
            {
                ToKhaiMauDich tkmd = new ToKhaiMauDich();
                tkmd.ID = Convert.ToInt64(list[i]);
                tkmd.Load();
                HangMauDich hmd = new HangMauDich();
                hmd.TKMD_ID = tkmd.ID;
                tkmd.HMDCollection = hmd.SelectCollectionBy_TKMD_ID();
                //tokhaiList.AppendChild(tkmd.ConvertToKhaiToXMLNHAP(tkmd.HMDCollection));
            }
           

            WS.FPT.SXXK.SXXKService service = new Company.BLL.WS.FPT.SXXK.SXXKService();
            string kq = service.DataProcess(doc.InnerXml, "ecusxgeiyklgA\\SmqUK@rbiqDyAgbXBIlBTUTL;", pass);
            return kq;

        }

         #endregion

        #region Hủy khai báo tờ khai nhập

        //huy dang ky
        public string WSHuyNhanXMLFPT(string pass)
        {
            XmlDocument doc = new XmlDocument();
            doc.Load("FPTService\\SXXK_TKN_HUY.xml");

            //luu thong tin gui
            XmlNode hqNhan = doc.SelectSingleNode("Root/SXXK/THONG_TIN/HQ_NHAN");
            hqNhan.Attributes["MA_HQ"].Value = this.MaHaiQuan;
            hqNhan.Attributes["TEN_HQ"].Value = Company.BLL.DuLieuChuan.DonViHaiQuan.GetName(this.MaHaiQuan);

            XmlNode nplNode = doc.SelectSingleNode("Root/SXXK/THONG_TIN/DU_LIEU");
            nplNode.Attributes["SO_DK"].Value = this.SoTiepNhan.ToString() ;
            WS.FPT.SXXK.SXXKService service = new Company.BLL.WS.FPT.SXXK.SXXKService();
            string kq = service.DataProcess(doc.InnerXml, "ecusxgeiyklgA\\SmqUK@rbiqDyAgbXBIlBTUTL;", pass);
            return kq;
        }

        #endregion Hủy khai báo danh sách nguyên phụ liệu

        #region Lấy thông tin trả lời từ hải quan nhận

        //lây thong tin dang ky
        public string WSRequestNhanXMLFPT(string pass)
        {
            XmlDocument doc = new XmlDocument();
            doc.Load("FPTService\\SXXK_TRALOI_DK.xml");

            //luu thong tin gui
            XmlNode hq = doc.SelectSingleNode("Root/SXXK");
            hq.Attributes["TRA_LOI"].Value = "DANG_KY_TKN";

            XmlNode hqNhan = doc.SelectSingleNode("Root/SXXK/THONG_TIN/HQ_GUI");
            hqNhan.Attributes["MA_HQ"].Value = this.MaHaiQuan;
            hqNhan.Attributes["TEN_HQ"].Value = Company.BLL.DuLieuChuan.DonViHaiQuan.GetName(this.MaHaiQuan);

            XmlNode dvGui = doc.SelectSingleNode("Root/SXXK/THONG_TIN/DON_VI_NHAN");
            dvGui.Attributes["MA_DV"].Value = this.MaDoanhNghiep;
            dvGui.Attributes["TEN_DV"].Value = "";
      
            WS.FPT.SXXK.SXXKService service = new Company.BLL.WS.FPT.SXXK.SXXKService();

            XmlNode data = doc.SelectSingleNode("Root/SXXK/DU_LIEU");
            data.Attributes["SO_DK"].Value = this.SoTiepNhan.ToString();
            string kq = service.DataProcess(doc.InnerXml, "ecusxgeiyklgA\\SmqUK@rbiqDyAgbXBIlBTUTL;", pass);
            return kq;
        }

        #endregion Lấy thông tin trả lời từ hải quan


        #region Khai báo tờ khai xuất

        //cap nhat mot to khai
        public string WSUpdateXMLXUATFPT(string pass, HangMauDichCollection collection)
        {
            XmlDocument doc = new XmlDocument();
            doc.Load(@"FPTService\SXXK_TKX_CAPNHAT.xml");

            //luu vao string
            XmlNode hqNhan = doc.SelectSingleNode("Root/SXXK/THONG_TIN/HQ_NHAN");
            hqNhan.Attributes["MA_HQ"].Value = this.MaHaiQuan;
            hqNhan.Attributes["TEN_HQ"].Value = Company.BLL.DuLieuChuan.DonViHaiQuan.GetName(this.MaHaiQuan);
            XmlNode data = doc.SelectSingleNode("Root/SXXK/DU_LIEU");
            data.Attributes["SO_DK"].Value = this.SoTiepNhan.ToString();
            //thong tin to khai
            XmlNode tokhai = doc.SelectSingleNode("Root/SXXK/DU_LIEU/TO_KHAI_XK/TO_KHAI");
            doc.RemoveChild(tokhai);
            XmlNode tokhaiList = doc.SelectSingleNode(@"Root/SXXK/DU_LIEU/TO_KHAI_XK");
            tokhaiList.AppendChild(ConvertToKhaiToXMLXUAT(collection));

            WS.FPT.SXXK.SXXKService service = new Company.BLL.WS.FPT.SXXK.SXXKService();
            string kq = service.DataProcess(doc.InnerXml, "ecusxgeiyklgA\\SmqUK@rbiqDyAgbXBIlBTUTL;", pass);
            return kq;

        }

        //dang ky
        public string WSSendXMLXUATFPT(string pass)
        {
            XmlDocument doc = new XmlDocument();
            doc.Load(@"FPTService\SXXK_TKX_DK.xml");

            //luu vao string
            XmlNode hqNhan = doc.SelectSingleNode("Root/SXXK/THONG_TIN/HQ_NHAN");
            hqNhan.Attributes["MA_HQ"].Value = this.MaHaiQuan;
            hqNhan.Attributes["TEN_HQ"].Value = Company.BLL.DuLieuChuan.DonViHaiQuan.GetName(this.MaHaiQuan);
            //thong tin to khai
            XmlNode tokhai = doc.SelectSingleNode("Root/SXXK/DU_LIEU/TO_KHAI_XK/TO_KHAI");
            doc.RemoveChild(tokhai);
            XmlNode tokhaiList = doc.SelectSingleNode(@"Root/SXXK/DU_LIEU/TO_KHAI_XK");
            tokhaiList.AppendChild(ConvertToKhaiToXMLXUAT(this.HMDCollection));

            WS.FPT.SXXK.SXXKService service = new Company.BLL.WS.FPT.SXXK.SXXKService();
            string kq = service.DataProcess(doc.InnerXml, "ecusxgeiyklgA\\SmqUK@rbiqDyAgbXBIlBTUTL;", pass);
            return kq;

        }

        //dang ky nhieu to khai
        public static string WSSendXMLXUATFPT(string pass, string[] list)
        {
            XmlDocument doc = new XmlDocument();
            doc.Load(@"FPTService\SXXK_TKX_DK.xml");

            //luu vao string
            ToKhaiMauDich tkmd1 = new ToKhaiMauDich();
            tkmd1.ID = Convert.ToInt64(list[0]);
            tkmd1.Load();
            XmlNode hqNhan = doc.SelectSingleNode("Root/SXXK/THONG_TIN/HQ_NHAN");
            hqNhan.Attributes["MA_HQ"].Value = tkmd1.MaHaiQuan;
            hqNhan.Attributes["TEN_HQ"].Value = Company.BLL.DuLieuChuan.DonViHaiQuan.GetName(tkmd1.MaHaiQuan);
            //thong tin to khai
            XmlNode tokhai = doc.SelectSingleNode("Root/SXXK/DU_LIEU/TO_KHAI_XK/TO_KHAI");
            doc.RemoveChild(tokhai);
            XmlNode tokhaiList = doc.SelectSingleNode("Root/SXXK/DU_LIEU/TO_KHAI_XK");
            for (int i = 0; i < list.Length; ++i)
            {
                ToKhaiMauDich tkmd = new ToKhaiMauDich();
                tkmd.ID = Convert.ToInt64(list[i]);
                tkmd.Load();
                HangMauDich hmd = new HangMauDich();
                hmd.TKMD_ID = tkmd.ID;
                tkmd.HMDCollection = hmd.SelectCollectionBy_TKMD_ID();
                //tokhaiList.AppendChild(tkmd.ConvertToKhaiToXMLNHAP(tkmd.HMDCollection));
            }


            WS.FPT.SXXK.SXXKService service = new Company.BLL.WS.FPT.SXXK.SXXKService();
            string kq = service.DataProcess(doc.InnerXml, "ecusxgeiyklgA\\SmqUK@rbiqDyAgbXBIlBTUTL;", pass);
            return kq;

        }

        #endregion

        #region Hủy khai báo tờ khai xuất

        //huy dang ky
        public string WSHuyXuatXMLFPT(string pass)
        {
            XmlDocument doc = new XmlDocument();
            doc.Load("FPTService\\SXXK_TKX_HUY.xml");

            //luu thong tin gui
            XmlNode hqNhan = doc.SelectSingleNode("Root/SXXK/THONG_TIN/HQ_NHAN");
            hqNhan.Attributes["MA_HQ"].Value = this.MaHaiQuan;
            hqNhan.Attributes["TEN_HQ"].Value = Company.BLL.DuLieuChuan.DonViHaiQuan.GetName(this.MaHaiQuan);

            XmlNode nplNode = doc.SelectSingleNode("SXXK/THONG_TIN/DU_LIEU");
            nplNode.Attributes["SO_DK"].Value = this.SoTiepNhan.ToString();
            WS.FPT.SXXK.SXXKService service = new Company.BLL.WS.FPT.SXXK.SXXKService();
            string kq = service.DataProcess(doc.InnerXml, "ecusxgeiyklgA\\SmqUK@rbiqDyAgbXBIlBTUTL;", pass);
            return kq;
        }

        #endregion Hủy khai báo danh sách nguyên phụ liệu

        #region Lấy thông tin trả lời từ hải quan tờ khai xuất

        //lây thong tin dang ky
        public string WSRequestXuatXMLFPT(string pass)
        {
            XmlDocument doc = new XmlDocument();
            doc.Load("FPTService\\SXXK_TRALOI_DK.xml");

            //luu thong tin gui
            XmlNode hq = doc.SelectSingleNode("Root/SXXK");
            hq.Attributes["TRA_LOI"].Value = "DANG_KY_TKX";

            XmlNode hqNhan = doc.SelectSingleNode("Root/SXXK/THONG_TIN/HQ_NHAN");
            hqNhan.Attributes["MA_HQ"].Value = this.MaHaiQuan;
            hqNhan.Attributes["TEN_HQ"].Value = Company.BLL.DuLieuChuan.DonViHaiQuan.GetName(this.MaHaiQuan);

            WS.FPT.SXXK.SXXKService service = new Company.BLL.WS.FPT.SXXK.SXXKService();

            XmlNode data = doc.SelectSingleNode("Root/DU_LIEU");
            data.Attributes["SO_DK"].Value = this.SoTiepNhan.ToString();
            string kq = service.DataProcess(doc.InnerXml, "ecusxgeiyklgA\\SmqUK@rbiqDyAgbXBIlBTUTL;", pass);
            return kq;
        }

        #endregion Lấy thông tin trả lời từ hải quan
        #endregion Webservice của FPT
    }
}